package com.threadExample;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by yijy on 2017/7/27.
 */
public class Syntest1 extends Thread{
//    private static int ticket=100;
    private static AtomicInteger ticket = new AtomicInteger(100);
    private boolean keeprunning=true;
    private static AtomicInteger ti=new AtomicInteger(0);
    @Override
    public void run() {
        go();
    }

    public void go(){
        try {
            while(keeprunning){
                if(ticket.intValue()>0){
                    ticket.getAndDecrement();
                    System.out.println(Thread.currentThread().getName()+"取票成功，剩余"+ticket+"张票");
                    Thread.sleep(500);
                    ti.getAndIncrement();
                }else{
                    System.out.println(Thread.currentThread().getName()+"取票失败，票已售罄！");
                    keeprunning=false;
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Syntest1 syntest1= new Syntest1();
        Thread t1 = new Thread(syntest1,"窗口1");
        Thread t2 = new Thread(syntest1,"窗口2");
        Thread t3 = new Thread(syntest1,"窗口3");
        Thread t4 = new Thread(syntest1,"窗口4");
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        try {
            t1.join();
            t2.join();
            t3.join();
            t4.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(ti);
    }


}

